function [ERRZ,jj] = computerEssentialErrorSVDNFramesGeneralMODTRACE(x,F,weights,w,h,outputformat)


n=size(F,2);

ERRZ=zeros(n,1);

if(sum(weights)<eps)
    weights=ones(n,1)/n;
    display('weights were all zero, resetting to one');
end

for i=1:n
    ERRZ(i,1) =weights(i,1)* computerEssentialErrorSVDTRACE(F{1,i},x,w,h);
end

if(outputformat==2) % convert to match the size of the variables
    D=ones(size(x,2),1)*sum((ERRZ));
    ERRZ=D;
elseif(outputformat==3) % scalar value
    D=sum((ERRZ));
    ERRZ=D;
end

jj= computerEssentialJACOBIANSVDTRACE(x,F,weights,w,h,outputformat);

% if nargout > 1
%   jj= computerEssentialJACOBIANSVDTRACE(x,F,weights,w,h,outputformat);
% end
end

function [F] = computerEssentialErrorSVDTRACE(MYF,x,w,h)



[foc1,foc2,xc,yc,skew] = reformatK(x,w,h);
F=(2*(foc1^2*(MYF(3,1) + MYF(1,1)*xc + MYF(2,1)*yc)^2 + (MYF(3,3) + MYF(1,3)*xc + MYF(3,1)*xc + MYF(2,3)*yc + MYF(3,2)*yc + MYF(1,1)*xc^2 + MYF(2,2)*yc^2 + MYF(1,2)*xc*yc + MYF(2,1)*xc*yc)^2 + (MYF(3,2)*foc2 + MYF(3,1)*skew + MYF(1,2)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(2,1)*skew*yc)^2)^2 - ((MYF(2,2)*foc2^2 + MYF(1,1)*skew^2 + MYF(1,2)*foc2*skew + MYF(2,1)*foc2*skew)^2 + foc1^2*(MYF(1,2)*foc2 + MYF(1,1)*skew)^2 + foc1^2*(MYF(2,1)*foc2 + MYF(1,1)*skew)^2 + foc1^2*(MYF(1,3) + MYF(1,1)*xc + MYF(1,2)*yc)^2 + foc1^2*(MYF(3,1) + MYF(1,1)*xc + MYF(2,1)*yc)^2 + (MYF(3,3) + MYF(1,3)*xc + MYF(3,1)*xc + MYF(2,3)*yc + MYF(3,2)*yc + MYF(1,1)*xc^2 + MYF(2,2)*yc^2 + MYF(1,2)*xc*yc + MYF(2,1)*xc*yc)^2 + (MYF(2,3)*foc2 + MYF(1,3)*skew + MYF(2,1)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(1,2)*skew*yc)^2 + (MYF(3,2)*foc2 + MYF(3,1)*skew + MYF(1,2)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(2,1)*skew*yc)^2 + MYF(1,1)^2*foc1^4)^2 + 4*((MYF(2,1)*foc2 + MYF(1,1)*skew)*(MYF(3,1) + MYF(1,1)*xc + MYF(2,1)*yc)*foc1^2 + (MYF(2,3)*foc2 + MYF(1,3)*skew + MYF(2,1)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(1,2)*skew*yc)*(MYF(3,3) + MYF(1,3)*xc + MYF(3,1)*xc + MYF(2,3)*yc + MYF(3,2)*yc + MYF(1,1)*xc^2 + MYF(2,2)*yc^2 + MYF(1,2)*xc*yc + MYF(2,1)*xc*yc) + (MYF(2,2)*foc2^2 + MYF(1,1)*skew^2 + MYF(1,2)*foc2*skew + MYF(2,1)*foc2*skew)*(MYF(3,2)*foc2 + MYF(3,1)*skew + MYF(1,2)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(2,1)*skew*yc))^2 + 4*(foc1*(MYF(1,2)*foc2 + MYF(1,1)*skew)*(MYF(2,2)*foc2^2 + MYF(1,1)*skew^2 + MYF(1,2)*foc2*skew + MYF(2,1)*foc2*skew) + MYF(1,1)*foc1^3*(MYF(2,1)*foc2 + MYF(1,1)*skew) + foc1*(MYF(1,3) + MYF(1,1)*xc + MYF(1,2)*yc)*(MYF(2,3)*foc2 + MYF(1,3)*skew + MYF(2,1)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(1,2)*skew*yc))^2 + 2*foc1^4*(MYF(1,1)^2*foc1^2 + MYF(1,1)^2*skew^2 + MYF(1,1)^2*xc^2 + 2*MYF(1,1)*MYF(1,2)*foc2*skew + 2*MYF(1,1)*MYF(1,2)*xc*yc + 2*MYF(1,1)*MYF(1,3)*xc + MYF(1,2)^2*foc2^2 + MYF(1,2)^2*yc^2 + 2*MYF(1,2)*MYF(1,3)*yc + MYF(1,3)^2)^2 + 2*((MYF(2,2)*foc2^2 + MYF(1,1)*skew^2 + MYF(1,2)*foc2*skew + MYF(2,1)*foc2*skew)^2 + foc1^2*(MYF(2,1)*foc2 + MYF(1,1)*skew)^2 + (MYF(2,3)*foc2 + MYF(1,3)*skew + MYF(2,1)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(1,2)*skew*yc)^2)^2 + 4*(foc1*(MYF(1,3) + MYF(1,1)*xc + MYF(1,2)*yc)*(MYF(3,3) + MYF(1,3)*xc + MYF(3,1)*xc + MYF(2,3)*yc + MYF(3,2)*yc + MYF(1,1)*xc^2 + MYF(2,2)*yc^2 + MYF(1,2)*xc*yc + MYF(2,1)*xc*yc) + MYF(1,1)*foc1^3*(MYF(3,1) + MYF(1,1)*xc + MYF(2,1)*yc) + foc1*(MYF(1,2)*foc2 + MYF(1,1)*skew)*(MYF(3,2)*foc2 + MYF(3,1)*skew + MYF(1,2)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(2,1)*skew*yc))^2)/((MYF(2,2)*foc2^2 + MYF(1,1)*skew^2 + MYF(1,2)*foc2*skew + MYF(2,1)*foc2*skew)^2 + foc1^2*(MYF(1,2)*foc2 + MYF(1,1)*skew)^2 + foc1^2*(MYF(2,1)*foc2 + MYF(1,1)*skew)^2 + foc1^2*(MYF(1,3) + MYF(1,1)*xc + MYF(1,2)*yc)^2 + foc1^2*(MYF(3,1) + MYF(1,1)*xc + MYF(2,1)*yc)^2 + (MYF(3,3) + MYF(1,3)*xc + MYF(3,1)*xc + MYF(2,3)*yc + MYF(3,2)*yc + MYF(1,1)*xc^2 + MYF(2,2)*yc^2 + MYF(1,2)*xc*yc + MYF(2,1)*xc*yc)^2 + (MYF(2,3)*foc2 + MYF(1,3)*skew + MYF(2,1)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(1,2)*skew*yc)^2 + (MYF(3,2)*foc2 + MYF(3,1)*skew + MYF(1,2)*foc2*xc + MYF(2,2)*foc2*yc + MYF(1,1)*skew*xc + MYF(2,1)*skew*yc)^2 + MYF(1,1)^2*foc1^4)^2;
end


